package com.smfx.yxt.controller.teacher;

import java.io.File;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.smfx.yxt.baidu.aip.AipFaceClient;
import com.smfx.yxt.baidu.aip.AipImageType;
import com.smfx.yxt.baidu.model.AipFaceDetect;
import com.smfx.yxt.common.AppUser;
import com.smfx.yxt.common.Const;
import com.smfx.yxt.common.MenuConst;
import com.smfx.yxt.common.RspResult;
import com.smfx.yxt.common.enums.RelationType;
import com.smfx.yxt.common.enums.Status;
import com.smfx.yxt.controller.BaseController;
import com.smfx.yxt.model.TeacherInfo;
import com.smfx.yxt.reqVo.HkFaceReqVO;
import com.smfx.yxt.reqVo.ParentReqVO;
import com.smfx.yxt.service.IParentService;
import com.smfx.yxt.service.ITeacherService;
import com.smfx.yxt.utils.BeanUtils;
import com.smfx.yxt.utils.HkFaceUtil;
import com.smfx.yxt.view.ParentRelStudentView;
import com.smfx.yxt.view.TeacherInfoView;
import com.smfx.yxt.vo.FaceCollectionVO;
import com.smfx.yxt.wechat.utils.FileUtil;

@RequestMapping(Const.TEACHER_ROLE + MenuConst.USER_MY_URL)
@Controller
public class TeacherUserController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(TeacherUserController.class);
	@Autowired
	private ITeacherService teacherService;
	@Autowired
	private IParentService parentService;
	
	@RequestMapping(value={"","/"})
	public String my(){
		return "teacher/my";
	}
	
	@RequestMapping("info")
	public String teacherInfo() {
		return "teacher/info";
	}
	
	@RequestMapping("resourceRefresh")
	public String resourceRefresh() {
		session.invalidate();
		return "teacher/index";
	}
	
	@RequestMapping("faceCollection")
	public String faceCollection(Model model) {
		TeacherInfo teacherInfo = teacherService.selectById(appUser().getTeacher().getId());
		model.addAttribute("teacher", teacherInfo);
		return "teacher/teacherFaceCollection";
	}
	
	/**
	 * 根据教师id查询教师信息
	 * @param teacherId
	 * @return
	 */
	@RequestMapping("info/edit")
	public String infoEdit(Model model) {
		AppUser appUser = getAppUser();
		TeacherInfo teacherInfo = teacherService.selectById(appUser.getTeacher().getId());
		TeacherInfoView infoView = BeanUtils.copyAs(teacherInfo, TeacherInfoView.class);
		model.addAttribute("teacher", infoView);
		return "teacher/teacherDetail";
	}
	
	@RequestMapping("teacher/chatList")
	public String teacherChatList() {
		return "teacher/chatList";
	}
	
	@RequestMapping("faceUpload")
	@ResponseBody Object faceUpload(FaceCollectionVO vo) {
		AipFaceDetect faceDetect = new AipFaceDetect(); 
		AppUser appUser = getAppUser();
		try {
			String path = RelationType.FACE_IMG.getCode() + File.separator;
			String fileName = "";
			path += "teacher" + File.separator + vo.getGroupName();
			fileName = appUser.getTeacher().getName() + "_JS" + appUser.getTeacher().getId() + ".JPG";
			faceDetect = AipFaceClient.apiFaceDetect(
					vo.getFaceUrl(), AipImageType.BASE64.getKey());
			if(!"0".equals(faceDetect.getError_code())) {
				return new RspResult(Status.FAIL, faceDetect);
			}
			TeacherInfo  teacherInfo = teacherService.selectById(appUser.getTeacher().getId());
			
			HkFaceReqVO hkVO = new HkFaceReqVO();
			hkVO.setCertificateType("居民身份证");
			hkVO.setCertificateNum(teacherInfo.getIdNo());
			hkVO.setName(teacherInfo.getName());
			hkVO.setFaceBinaryData(vo.getFaceUrl());
			hkVO.setFaceGroupCode(vo.getGroupCode());
			if(StringUtils.isBlank(teacherInfo.getFaceIndexCode())) {
				String result = HkFaceUtil.addSingleFace(hkVO);
				JSONObject object = JSONObject.parseObject(result);
				if("0".equals(object.getString("code"))) {
					JSONObject dataObject = object.getJSONObject("data");
					teacherInfo.setFaceIndexCode(dataObject.getString("indexCode"));
				}else {
					faceDetect.setError_code(object.getString("code"));
					faceDetect.setError_msg(object.getString("msg"));
					return new RspResult(Status.FAIL, faceDetect);
				}
			}else {
				if(!teacherInfo.getFaceGroupCode().equals(vo.getGroupCode())) {
					String result = HkFaceUtil.deleteSingleFace(
							teacherInfo.getFaceGroupCode(), teacherInfo.getFaceIndexCode());
					JSONObject object = JSONObject.parseObject(result);
					if(!"0".equals(object.getString("code"))) {
						faceDetect.setError_code(object.getString("code"));
						faceDetect.setError_msg(object.getString("msg"));
						return new RspResult(Status.FAIL, faceDetect);
					}else {
						File file = new File(Const.rootPath + File.separator + teacherInfo.getFaceImgUrl());
						if(file.exists()) {
							file.delete();
						}
						result = HkFaceUtil.addSingleFace(hkVO);
						object = JSONObject.parseObject(result);
						if("0".equals(object.getString("code"))) {
							JSONObject dataObject = object.getJSONObject("data");
							teacherInfo.setFaceIndexCode(dataObject.getString("indexCode"));
						}else {
							faceDetect.setError_code(object.getString("code"));
							faceDetect.setError_msg(object.getString("msg"));
							return new RspResult(Status.FAIL, faceDetect);
						}
					}
				}else {
					hkVO.setFaceIndexCode(teacherInfo.getFaceIndexCode());
					String result = HkFaceUtil.updateSingleFace(hkVO);
					JSONObject object = JSONObject.parseObject(result);
					if(!"0".equals(object.getString("code"))) {
						faceDetect.setError_code(object.getString("code"));
						faceDetect.setError_msg(object.getString("msg"));
						return new RspResult(Status.FAIL, faceDetect);
					}
				}
			}
			faceDetect = FileUtil.saveFaceImg(vo.getFaceUrl(), path, fileName);
			teacherInfo.setFaceImgUrl(faceDetect.getFileUrl());
			teacherInfo.setFaceGroupCode(vo.getGroupCode());
			teacherService.updateById(teacherInfo);
			return new RspResult(Status.SUCCESS, faceDetect);
		} catch (Exception e) {
			logger.error("--faceUpload error={}", e);
			faceDetect.setError_code(Status.FAIL.getCode());
			faceDetect.setError_msg("上传异常");
		}
		return new RspResult(Status.FAIL, faceDetect);
	}
}
